<?php
require_once 'BaseController.php';

class Admin_InfoController extends BaseController {
	
	//信息列表
	public function infoListAction() {
		try {
			$infoDAO = $this->orm->createDAO('_wenda_site_info')->order('info_order desc,info_id desc');
			$infoDAO->getPager(array('path'=>'/info'))->assignTo($this->view);
			
			echo $this->view->render('info/info-list.htm');
			exit;
		}catch(Exception $e) {
			$this->toErrorLogs($e);
			$this->alert_back(addslashes($e->getMessage()));
		}
	}
	
	//添加信息
	public function addInfoAction() {
		try {
			if($_POST) {
				$title = HttpUtil::postString('info_title');
				$content = HttpUtil::postString('info_content');
				$order = HttpUtil::postString('info_order');
				
				$infoDAO = $this->orm->createDAO('_wenda_site_info');
				$infoDAO->info_title = $title;
				$infoDAO->info_content = $content;
				$infoDAO->info_order = $order;
				
				$infoDAO->save();
				
				$this->alert_go('添加成功！','/admin/info/info-list');
			}
			
			echo $this->view->render('info/add-info.htm');
			exit;
		}catch(Exception $e) {
			$this->toErrorLogs($e);
			$this->alert_back(addslashes($e->getMessage()));
		}
	}
	
	//编辑信息
	public function editInfoAction() {
		try {
			$iid = HttpUtil::getString('iid');
			$infoDAO = $this->orm->createDAO('_wenda_site_info')->findInfo_id($iid);
			
			if($_POST) {
				$title = HttpUtil::postString('info_title');
				$content = HttpUtil::postString('info_content');
				$order = HttpUtil::postString('info_order');
				
				$infoDAO->info_title = $title;
				$infoDAO->info_content = $content;
				$infoDAO->info_order = $order;
				
				$infoDAO->save();
				
				$this->alert_go('修改成功！','/admin/info/info-list');
			}
			
			$this->view->assign(array(
				'data'		=> $infoDAO->get()
			));
			
			echo $this->view->render('info/add-info.htm');
			exit;
		}catch(Exception $e) {
			$this->toErrorLogs($e);
			$this->alert_back(addslashes($e->getMessage()));
		}
	}
	
	//删除信息
	public function deleteInfoAction() {
		try {
			$iid = HttpUtil::getString('iid');
			$this->orm->createDAO('_wenda_site_info')->findInfo_id($iid)->delete();
			
			$this->alert_go('删除成功！',$_SERVER['HTTP_REFERER']);
		}catch(Exception $e) {
			$this->toErrorLogs($e);
			$this->alert_back(addslashes($e->getMessage()));
		}
	}
	
	//设置信息状态
	public function setTypeAction() {
		try {
			$info = HttpUtil::getString('info');
			$info = explode('_',$info);
			
			$infoDAO = $this->orm->createDAO('_wenda_site_info')->findInfo_id($info[0]);
			$infoDAO->is_post = intval($info[1]);
			
			$infoDAO->save();
			
			$this->alert_go('设置成功！',$_SERVER['HTTP_REFERER']);
		}catch(Exception $e) {
			$this->toErrorLogs($e);
			$this->alert_back(addslashes($e->getMessage()));
		}
	}
}